﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using T.ApiResults;
using T.Vehicleservice.API.Application.Command.MaintenanceRecordCommand;
using T.Vehicleservice.API.Application.Command.ViolationRecordCommand;
using T.Vehicleservice.Domain.T.MaintenanceRecord.Model;
using T.Vehicleservice.Domain.T.ViolationRecords.Model;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;

namespace T.Vehicleservice.API.Controllers
{
    /// <summary>
    /// 违章记录控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ViolationRecordController : ControllerBase
    {
       
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator _mediator;
        public ViolationRecordController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 添加违章信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> CreateViolationRecor(ViolationRecordCreateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                command.CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//公司ID
                command.User_Id = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value);//用户ID
                command.CreatedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//创建人
                result.Code = await _mediator.Send(command);
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "添加违章信息失败";
                throw;
            }
            return result;
        }
        /// <summary>
        /// 删除违章信息（批删）
        /// </summary>
        /// <param name="Command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DeleteAllViolationRecord(ViolationRecordDeleteAllCommand Command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                Command.DeletedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//删除人
                Command.DeletedDate = DateTime.Now;//删除时间
                result.Code = await _mediator.Send(Command);

            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "删除违章信息（批删）失败";
                throw;
            }
            return result;
        }
        /// <summary>
        /// 删除违章信息（单删）
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DeleteViolationRecord([FromQuery] ViolationRecordDeleteCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {

                command.DeletedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//删除人
                command.DeletedDate = DateTime.Now;//删除时间
                result.Code = await _mediator.Send(command);
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "删除违章信息（单删）失败";
                throw;
            }
            return result;
        }
        /// <summary>
        /// 反填违章信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<ViolationRecord>> ViolationRecordGetBy([FromQuery] ViolationRecordGetByCommand command)
        {
            ApiResult<ViolationRecord> result = new ApiResult<ViolationRecord>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 修改违章信息
        /// </summary>
        /// <param name="ViolationRecordUpdateCommand"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateViolationRecord(ViolationRecordUpdateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                command.CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//公司ID
                command.User_Id = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value);//用户ID
                command.UpdatedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//修改人
                command.UpdatedDate = DateTime.Now;//修改时间
                result.Code = await _mediator.Send(command);


            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "修改违章信息失败";
                throw;
            }
            return result;

        }
    }
}
